package com.itheima.controller.response;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * HelloController: 能够对客户端请求进行响应,springmvc中叫做控制器controller或者处理器Handler
 */
@Controller
public class ResponseExceptionController {
    /**
     * @TODO 需求：当前端页面发送http://localhost:8080/exception请求时，后端程序执行时抛出异常，请你捕获异常并跳转到异常友好页面error.jsp
     * 分析：如果在每一个方法中写try catch 这样写代码存在耦合性。
     */
    @RequestMapping(value = "/exception", method = RequestMethod.GET)
    public String exception(Model model) {
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            model.addAttribute("error", "系统繁忙,请稍后再试!");
            return "error";
        }
        return "success";
    }

    /**
     * springmvc处理异常：
     * 1.不用在程序中写try catch
     * 2.使用注解可以捕获异常
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/exception2", method = RequestMethod.GET)
    public String exception2(Model model) {
        int i = 10 / 0;
        return "success";
    }

}
